const express = require('express')
const c = require('../connection')
const router = express.Router()
//  工单车牌号和工单号去重接口
router.get('/distinct', (req, res) => {
    var obj = req.query
    obj.ordernum == 'undefined' ? '' : obj.ordernum
    obj.ocar == 'undefined' ? '' : obj.ocar
    c.query(`select distinct ordernum,ocar from cs_order`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '查询失败',
                data: r
            })
        }
    })
})
//  工单模糊查询接口
router.get('/', (req, res) => {
    var obj = req.query
    obj.ordernum == 'undefined' ? '' : obj.ordernum
    obj.ocar == 'undefined' ? '' : obj.ocar
    c.query(`select * from cs_order,cs_partmessage,cs_coa where ocode=pcode and pcategory=aid and ordernum like ? and ocar like ? order by oid`, [`%${obj.ordernum}%`, [`%${obj.ocar}%`]], (err, r) => {
        console.log(r);
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '查询失败',
                data: r
            })
        }
    })
})
//  维修项目新增项目接口
router.post('/add', (req, res) => {
    var obj = req.body
    c.query(`insert into cs_order set ?`, [obj], (err, r) => {
        if (err) {
            throw err
        }
        console.log(r);
        res.send({
            code: 200,
            msg: '工单数据新增成功'
        })
    })
})
//  工单维修项目修改接口
router.post('/update', (req, res) => {
    var obj = req.body
    c.query(`update cs_order set ? where oid=?`, [obj, obj.oid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows > 0) {
            res.send({
                code: 200,
                msg: '修改项目成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '修改项目失败',
                data: r
            })
        }
    })
})
//  维修项目删除接口
router.post('/delete', (req, res) => {
    var obj = req.body
    c.query(`delete from cs_order where oid=?`, [obj.oid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.affectedRows === 1) {
            res.send({
                code: 200,
                msg: '删除成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '删除失败'
            })
        }
    })
})
//  维修项目入账时间接口
router.post('/uptime', (req, res) => {
    var obj = req.body
    c.query(`update cs_order set ? where ordernum=?`, [obj, obj.ordernum], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows > 0) {
            res.send({
                code: 200,
                msg: '入账成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '入账失败',
                data: r
            })
        }
    })
})
module.exports = router;